#!/usr/bin/env bash
# 判断当前包管理器
pac='yum'
if [[ `which apt` -eq 0 ]];then
	pac='apt'
fi
RestartService(){
	systemctl daemon-reload
	systemctl restart ssh
	systemctl restart sshd
}
status=1
RestartService
sleep 10
netstat -ntlp | grep sshd
if [[ $? -eq 0 ]];then
	echo "Service Is Running"
	status=0
else
	if [[ `which lsof` -eq 0 ]];then
		lsof -i:22
		if [[ $? -eq 0 ]];then
			echo "Port Is Listen"
			status=0
		else
			status=1
		fi
	fi
fi

if [[ $status == 0 ]];then
	exit 0
fi
# 清除包缓存
$pac clean
$pac install openssh-server -y
if [[ $? -eq 0 ]];then
	RestartService
else
	if [[ $pac == 'apt' ]];then
		apt update
	else
		yum makecache
	fi
 	$pac install openssh-server -y
	if [[ $? -eq 0 ]];then
		RestartService
	fi
fi